<template>
	<div class="sys-dictType-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="仓库名称" prop="warehousCode" :rules="[{ required: true, message: '仓库名称不能为空', trigger: 'blur' }]">
		<el-select v-model="state.ruleForm.warehousCode" placeholder="请选择仓库">
  		<el-option 
		:label="state.warehouseList.some(item => item.code === state.ruleForm.warehousCode) 
 		 ? state.warehouseList.find(item => item.code === state.ruleForm.warehousCode)!.name 
 		 : '请选择仓库'"
  		  :value="state.ruleForm.warehousCode">
 	 	</el-option>
 	 	<el-option v-for="item in state.warehouseList" :key="item.code" :label="item.name" :value="item.code">
 		 </el-option>
		</el-select>
						</el-form-item>
					</el-col>
					
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						
						<el-form-item label="出入口" prop="opening" :rules="[{ required: true, message: '出入口不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.opening" placeholder="出入口" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="点位" prop="point" :rules="[{ required: true, message: '点位不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.point" placeholder="出入口点位" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="最近更新日期" prop="point" :rules="[{ required: true, message: '时间不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.updateTime" placeholder="更新时间" clearable disabled  />
						</el-form-item>
					</el-col>
					
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20" >
						<el-form-item label="是否冻结" prop="isenable" :rules="[{ required: false, message: '必选', trigger: 'blur' }]">
							<!-- v-if="state.ruleForm.name != null" 加在是否的item上 -->
							<el-radio-group v-model="state.ruleForm.isenable">
								<el-radio :value="1">是</el-radio>
								<el-radio :value="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="Editentrance">


import { getAPI } from '/@/utils/axios-utils';

import { EntranceApi } from '/@/api-services/apis/warehouse-entranceandexit-api'; // 导入 API 服务
import { EntranceAndexitInput,DeleteEntranceAndexit } from '/@/api-services/models/entranceandexit -input'; // 导入获取实体
import { AccountTypeEnum, UpdateDictTypeInput } from '/@/api-services/models';
import { useUserInfo } from '/@/stores/userInfo';
import { ref, onMounted,reactive } from 'vue';

import { WarehouseInput } from '/@/api-services/models/warehouse-input'; // 导入仓库实体添加编辑下拉
import { WarehouseApi } from '/@/api-services/apis/warehouse-api';
import { Message } from '@element-plus/icons-vue/dist/types';

// 打开弹窗
const openDialog = (row: any) => {
	state.ruleForm = JSON.parse(JSON.stringify(row));
	state.isShowDialog = true;
	ruleFormRef.value?.resetFields();

};

const props = defineProps({
	title: String,
});
const emits = defineEmits(['handleQuery']);
const ruleFormRef = ref();
const state = reactive({
	loading: false,
	isShowDialog: false,
	ruleForm: {} as EntranceAndexitInput,
	warehouseList :Array<WarehouseInput>(),

});
// 在 onMounted 生命周期钩子中获取仓库数据
onMounted(async () => {
    try {
        state.loading = true;
        // 调用 API 获取仓库数据
        const response = await getAPI(WarehouseApi).apiWarehousePagePost({ page: 1,
            pageSize: 1000, isEnable: 1 });
            state.warehouseList = response.data.result?.items||[];
            
    } catch (error) {
        console.error('获取仓库数据失败', error);
    }
});
// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		 //state.ruleForm.id != undefined && state.ruleForm.id > 0
		if (props.title && props.title.includes("编辑")) {

				console.log(state.ruleForm);		
			await getAPI(EntranceApi).apiEntranceUpdatePost(state.ruleForm);
			
		} else {
			
			await getAPI(EntranceApi).apiEntranceAddPost(state.ruleForm);
		}
		closeDialog();
	});
};

// 导出对象
defineExpose({ openDialog });
</script>
